package com.shuoyue.ycgk.ui.main.contract;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.Mok;
import com.shuoyue.ycgk.entity.MyBanner;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.entity.SpecialType;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public interface FragmentMainItemContract {
    interface View extends BaseView {
        void setBanners(ArrayList<MyBanner> banners);

        void setCourse(ArrayList<Course> course);

        void setSpecialList(ArrayList<SpecialType> specialList);

        void setMokList(ArrayList<Mok> mData);

        void setPaperSet(List<PaperSet> paperSets);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getBanners(int modelId) {
            apply(model.getBanner(modelId))
                    .subscribe(new ApiSubscriber<Optional<List<MyBanner>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<MyBanner>> listOptional) {
                            super.onNext(listOptional);
                            mView.setBanners((ArrayList<MyBanner>) listOptional.getIncludeNull());
                        }
                    });

        }

        public void getBestCourse(String moduleName) {
            apply(model.getBestCourse(moduleName))
                    .subscribe(new ApiSubscriber<Optional<List<Course>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Course>> listOptional) {
                            super.onNext(listOptional);
                            mView.setCourse((ArrayList<Course>) listOptional.getIncludeNull());
                        }
                    });

        }

        public void getSpecial(Integer moduleId, Integer subjectId) {
            apply(model.getSpecialList(moduleId, subjectId))
                    .subscribe(new ApiSubscriber<Optional<List<SpecialType>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<SpecialType>> listOptional) {
                            super.onNext(listOptional);
                            mView.setSpecialList((ArrayList<SpecialType>) listOptional.getIncludeNull());
                        }
                    });
        }

        public void getMok(int subjectId) {
            apply(model.getMok(subjectId))
                    .subscribe(new ApiSubscriber<Optional<List<Mok>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<Mok>> listOptional) {
                            super.onNext(listOptional);
                            mView.setMokList((ArrayList<Mok>) listOptional.getIncludeNull());
                        }
                    });
        }


        public void getPaperSet(int moduleId) {
            apply(model.getPaperSet(moduleId))
                    .subscribe(new ApiSubscriber<Optional<List<PaperSet>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<PaperSet>> listOptional) {
                            super.onNext(listOptional);
                            mView.setPaperSet(listOptional.getIncludeNull());
                        }
                    });
        }

    }

    class Model extends BaseModel {
        Observable<BaseResult<List<PaperSet>>> getPaperSet(int moduleId) {
            //TODO isHome改成1
            return RetrofitClient.getInstance().getApi().getPaperSetList(1, moduleId, null, null);
        }

        Observable<BaseResult<List<Mok>>> getMok(int subjectId) {
            return RetrofitClient.getInstance().getApi().getHomeMock(subjectId);
        }

        Observable<BaseResult<List<Course>>> getBestCourse(String modelName) {
            return RetrofitClient.getInstance().getApi().getGoodsCourseList(modelName);
        }

        Observable<BaseResult<List<MyBanner>>> getBanner(int modelId) {
            return RetrofitClient.getInstance().getApi().getHomeBanners(modelId);
        }

        Observable<BaseResult<List<SpecialType>>> getSpecialList(Integer moduleId, Integer subjectId) {
            return RetrofitClient.getInstance().getApi().getHomeSpecialList(moduleId, subjectId);
        }
    }
}
